package groovy.lang.webobjects;

import com.webobjects.appserver.WOApplication;
import com.webobjects.foundation.NSNotification;
import com.webobjects.foundation.NSNotificationCenter;
import com.webobjects.foundation.NSSelector;

/* 
 * Copyright (c) 2007 Quinton Dolan. . All Rights Reserved.
 *
 * Inspiration and some portions of code from  Suzuki Tetsuya's 
 * WOGroovy implementation (http://wogroovy.sourceforge.jp/)
 */

/**
 * WOGroovy provides additional WebObjects and Project Wonder GJDK style
 * functionality into the groovy MetaClass hierarchy
 */

public class WOGroovy {
	private static boolean initialized = false;
	public static boolean javaAgentRunning = false;
	public WOGroovy() {
		initialize();
	}
	
	public static void initialize() {
		if (initialized)
			return;
		initialized = true;
		WOGroovyClassLoader.registerClassLoader();
	}
	
	public static void initializeClassHandler() {
		WOGroovyClassHandler.getClassHandler().initialize();
	}

	public static Observer observer;

	public static class Observer {
		public void finishedLaunchingApp(@SuppressWarnings("unused")
		NSNotification n) {
			WOGroovy.initializeClassHandler();
		}
	}
	static {
		initialize();
		observer = new Observer();
		try {
			NSNotificationCenter.defaultCenter().addObserver(observer,
					new NSSelector("finishedLaunchingApp", 
							new Class[] { com.webobjects.foundation.NSNotification.class }),
					WOApplication.ApplicationWillFinishLaunchingNotification,
					null);
		} catch (Exception e) {
			System.out.println("Caught exception: " + e.getMessage() + " stack: ");
			e.printStackTrace();
		}
	}
}
